package com.neolix.watch.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.neolix.watch.NeolixNaviagationBaseActivity;
import com.neolix.watch.R;
import com.neolix.watch.adapter.BluetoothListAdapter;
import com.neolix.watch.util.Constant;
import com.neolix.watch.view.ConfirmDialog;

import java.util.ArrayList;
import java.util.List;

public class BluetoothListActivity extends NeolixNaviagationBaseActivity {

    public static final int REQUEST_CONNECT_BLUETOOTH = 1;
    private ListView deviceList;
    private BluetoothListAdapter adapter;
    private List<BluetoothDevice> devices;
    private boolean scanning;
    private BluetoothAdapter mBluetoothAdapter;
    private ConfirmDialog dialog;
    private TextView connectLaterTextView;

    public BluetoothListActivity() {
        super(R.layout.activity_bluetooth_list);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getString(R.string.connect_device));
        setLeftButtonVisibility(false);

        initUi();
        initData();
        initDialog();

        dialog.show();
        scanLeDevice(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CONNECT_BLUETOOTH && resultCode == RESULT_OK) {
            //连接成功
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        scanLeDevice(false);
    }

    private void initDialog() {
        dialog = new ConfirmDialog(this, true);
        dialog.setTitle(getString(R.string.scaning_bluetooth_device));

        dialog.displayWithStyle(ConfirmDialog.DisplayStyle.NEGATIVE);
        dialog.setNegativeButton(getString(R.string.cancel_scan), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scanLeDevice(false);
                dialog.dismiss();
            }
        });
    }

    private void initData() {
        devices = new ArrayList<BluetoothDevice>();
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
    }

    private void initUi() {
        deviceList = (ListView) findViewById(R.id.device_list);
        connectLaterTextView = (TextView) findViewById(R.id.connect_later_text_view);
        adapter = new BluetoothListAdapter(getApplicationContext());
        adapter.bindListView(deviceList);

        deviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getApplicationContext(), BluetoothConnectActivity.class);
                intent.putExtra(Constant.DEVICE_NAME, adapter.getItem(i).getAddress());
                startActivityForResult(intent, REQUEST_CONNECT_BLUETOOTH);
            }
        });

        connectLaterTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    private void scanLeDevice(final boolean enable) {
        if (enable) {
            scanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            scanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {

                @Override
                public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!isExistDevice(device)) {
                                devices.add(device);
                                adapter.setData(devices);
                            }
                        }
                    });
                }
            };

    private boolean isExistDevice(BluetoothDevice device) {
        for (BluetoothDevice device1 : devices) {
            if (device.getAddress().equals(device1.getAddress()))
                return true;
        }
        return false;
    }
}
